package com.gxa.p2p.controller;

import com.gxa.p2p.businessExceptionCode.ErrorCode;
import com.gxa.p2p.exception.BusinessException;
import com.gxa.p2p.pojo.LoginLog;
import com.gxa.p2p.service.LoginInfoService;
import com.gxa.p2p.util.ReturnMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 登录管理 当前用户登录信息查询
 *
 * @author Longlin_Feng @date 2020/8/1
 */
@Controller
@CrossOrigin
@RequestMapping("/loginInfo")
@Api(value = "用户登录历史信息查询", tags = "用户登录历史信息查询接口")
public class LoginInfoController {

  @Resource private LoginInfoService loginInfoService;

  /**
   * 查询当前系统用户登录信息
   *
   * @param currentUserId 当前用户id
   * @return Map<String , Object>
   */
  @GetMapping("/history")
  @ResponseBody
  @ApiOperation(value = "当前系统用户登录历史查询", httpMethod = "get", response = List.class)
  @ApiImplicitParam(
      name = "currentUserId",
      value = "当前用户id",
      required = true,
      paramType = "Integer")
  public List<LoginLog> findLoginHistory(@RequestParam("currentUserId") Integer currentUserId) {
    try {
      // 调用服务层接口,返回登录日志对象
      return loginInfoService.findLoginHistory(currentUserId);
    } catch (Exception e) {
      throw new BusinessException(ErrorCode.FAILED);
    }
  }

  /**
   * 查询当前用户登录信息
   * @return
   */
  @GetMapping("/findLogin")
  @ResponseBody
  @ApiOperation(value = "当前用户登录历史查询", httpMethod = "get", response = List.class)
  @ApiImplicitParam(
          name = "currentUserId",
          value = "当前用户id",
          required = true,
          paramType = "Integer")
  public Map<String, Object> findLogin(@RequestParam("userId") Integer currentUserId) {

    // 调用服务层接口,返回登录日志对象
    List<LoginLog> loginUser = loginInfoService.findLoginUser(currentUserId);
    ReturnMap<Object> returnMap = new ReturnMap<>();
    return returnMap.returnMap(loginUser);
  }

}
